"""MC-RES Form
See the pdf module for example usage.
"""


from reportlab.platypus import Paragraph, Table, Flowable, XPreformatted, Spacer
from reportlab.lib.units import inch
from reportlab.lib import colors
from settings import *
from title24_styles  import t24_styles, T24Styles
from title24_widgets import *


def get_flowables(**kwargs):
    """Returns a list of Flowables that represent the MCRES pages"""
    pages = []
    pages.append(MCRES1(**kwargs))
    return pages


################################################################################
##### MCRES-1
################################################################################
class MCRES1(Table):
    """MCRES (Page 1 of 1)"""
    row_hgts = (0.20*inch, 0.20*inch)
    def __init__(self, **kwargs):
        self.style_sheet = T24Styles(**kwargs)
        self.styles = self.style_sheet.styles
        self.styles['mcres_table'] = self.styles['tbl_1_pad'] + [
            ('TOPPADDING',(0,0),(-1,-1), 0),
            ('VALIGN',(0,0),(-1,-1),'MIDDLE'),
            ('FONTSIZE',(0,0),(-1,-1), 9),
            ('LEADING',(0,2),(-1,-1), 9*1.2),
            ('ALIGN',(0,0),(0,-1),'LEFT'),
            ('ALIGN',(1,0),(-1,-1),'CENTER'),
            #('INNERGRID',(0,0),(-1,-1),LINE_WIDTH,LINE_COLOR),
            ('BOX',(0,0),(-1,-1),LINE_WIDTH,LINE_COLOR),
        ]
        self.kwargs = kwargs
        parts = self.collect_parts()
        Table.__init__(self,
            data=parts,
            colWidths=(get_draw_width(),),
            style=self.styles['tbl_container'],
        )

    def title(self):
        styles = t24_styles()
        col1 = 1.0*inch
        col0 = get_draw_width()-col1
        b = "<b>%s</b>"
        return Table(
            data=(
                (
                    Paragraph(
                        b % "Residential Form: Marin County Energy Ordinance",
                        styles['title']
                    ),
                    Paragraph(b % "MC-RES", styles['title_right']),
                ),
                (label_pp(''), label_pp(''),),
            ),
            colWidths=(col0, col1),
            rowHeights=(HEADER_HEIGHT, 0.125*inch),
            style=styles['tbl_1_pad'] + [
                ('LINEBELOW',(0,0),(-1,0),1,colors.black),
            ],
        )

    def project_block(self):
        kw = self.kwargs
        c0 = 2.0*inch
        c1 = 1.5*inch
        c2 = 2.25*inch
        space = 0.125*inch
        project_name = Table(
            data=(
                (
                    Paragraph("Project Name/Address:",
                              self.styles['tag_medium']),
                    tag_medium(
                        kw.get('project_title', 'MISSING PROJECT TITLE'),
                        **kw
                    ),
                ),
            ),
            colWidths=(c0, get_draw_width()-c0),
            style=self.styles['mcres_table'],
        )

        date_and_cfa = Table(
            data = (
                (
                    Paragraph("Date of Title 24 Report:",
                              self.styles['tag_medium']),
                    tag_medium(
                        kw.get('date_str', 'MISSING DATE STR'),
                        **kw
                    ),
                    eighth_inch_spacer(),
                    Paragraph("Conditioned Floor Area (SF):",
                              self.styles['tag_medium']),
                    tag_medium(
                        kw.get('project_cfa', 'MISSING PROJECT CFA'),
                        **kw
                    ),
                ),
            ),
            colWidths=(c0, c1, space,
                       c2, get_draw_width() - (c0+c1+c2+space)),
            style=self.styles['mcres_table'],
        )
        return Table(
            data=((project_name,), (date_and_cfa,)),
            #colWidths=(get_draw_width()),
            style=self.styles['tbl_container'],
        )

    def std_design(self):
        kw = self.kwargs
        times = mc_label_pp('<b>x</b>')
        equals = mc_label_pp('<b>=</b>')
        cols = [
            1.8*inch, 0.25*inch, 1.8*inch, 0.25*inch, 1.8*inch
        ]
        data = (
            # Row 0
            #('', '', '', '', '', '',),
            # Row 1
            (
                h2_bold_pp(
                    "<u>1. Calculate Marin County Energy Ordinance " \
                    "Standard Design:</u>"
                ),'','','','','',
            ),
            # Row 2
            #('', '', '', '', '', '',),
            # Row 3
            (
                mc_label_pp(
                    '<b>Standard <super>1</super> Design <br /><u>Total TDV ' \
                    'KBtu/sf-yr</u></b>'
                ), '', mc_label_pp(
                    '<i>[Adjustment Factor from<br />Compliance Table below]</i>'
                ), '', mc_label_pp(
                    '<b>Adjusted<br />Standard Design<br /><u>Total TDV ' \
                    'KBtu/sf-yr</u></b>'
                ), ''
            ),
            # Row 4
            (
                tag_medium_center(
                        kw.get('standard_t24', 'MISSING STANDARD T24'),
                        **kw
                ), times, tag_medium_center(
                        kw.get('adjustment_factor_standard',
                               'MISSING ADJUSTMENT FACTOR STANDARD'), **kw),
                equals, tag_medium_center(
                        kw.get('adjusted_standard_design',
                               'MISSING ADJUSTED STANDARD DESIGN'), **kw),
                mc_label_pp(
                    '<i>[used in Step #4]</i>'
                ),
            ),
            # Row 5
            (
                '', times, tag_medium_center(
                        kw.get('adjustment_factor_proposed',
                               'MISSING ADJUSTMENT FACTOR PROPOSED'), **kw),
                equals, tag_medium_center(
                        kw.get('max_allowed_proposed',
                               'MISSING MAX ALLOWED PROPOSED'), **kw),
                mc_label_pp(
                    '<i>[used in Step #2]</i>'
                ),
            ),
            # Row 6
            (
                mc_label_pp(
                    '<i>[from Part1, CF-1R:<br />see Note 1 below]</i>'
                ), '', '', '', mc_label_pp(
                    '<b>Maximum Allowed<br />Proposed Design<br />(TDV ' \
                    'KBtu/sf-yr)</b>'
                ), ''
            ),
            # Row 7
            (
                notes_pp(
                    '<i><u>Note 1:</u> The Proposed Design and Standard ' \
                    'Design values must include DHW energy use, except in ' \
                    'the E+A+A method when there is no change in the ' \
                    'existing water heater(s).</i>'
                ), '', '', '', '', ''
            ),
        )
        return Table(
            data,
            colWidths=(get_remaining_width(cols)),
            style=self.styles['tbl_1_pad'] + [
                ('SPAN', (0,0),(-1,0)),
                ('SPAN', (0,2),(0,3)),
                ('VALIGN',(0,2),(0,3),'MIDDLE'),
                ('VALIGN',(0,4),(-1,4),'TOP'),
                ('BOX',(0,2),(0,3),LINE_WIDTH,LINE_COLOR),
                ('BOX',(2,2),(2,2),LINE_WIDTH,LINE_COLOR),
                ('BOX',(4,2),(4,2),LINE_WIDTH+1,LINE_COLOR),
                ('BOX',(4,3),(4,3),LINE_WIDTH+1,LINE_COLOR),
                ('SPAN', (0,5),(-1,5)),
            ],
        )

    def pv_credit(self):
        kw = self.kwargs
        minus = mc_label_pp('<b>-</b>')
        equals = mc_label_pp('<b>=</b>')
        cols = [
            1.8*inch, 2.3*inch, 1.8*inch
        ]
        data = (
            # Row 0
            ('', '', '', '',),
            # Row 1
            (
                h2_bold_pp(
                    "<u>2. Calculate Solar Photovoltaic (PV) System Credit:</u>"
                ),'','','',
            ),
            # Row 2
            (
                notes_pp(
                    '<i>For a solar PV system credit under the Ordinance, ' \
                    'Annual Production is obtained using the CEC PV ' \
                    'Calculator at:</i>'
                ), '', '', '',
            ),
            # Row 3
            (
                Paragraph(
                    '<i><link href="http://www.gosolarcalifornia.ca.gov/' \
                    'nshpcalculator/download_calculator.html">' \
                    'http://www.gosolarcalifornia.ca.gov/nshpcalculator/' \
                    'download_calculator.html</link></i>',
                    self.styles['anchor_note']
                ), '', '', '',
            ),
            # Row 4
            (
                mc_label_pp(
                    '<b><i><u>Solar PV Credit may be used only if Proposed ' \
                    'Design is = or &lt; the Maximum Allowed Proposed Design.' \
                    '</u></i></b>'
                ), '', '', '',
            ),
            # Row 5
            (
                mc_label_pp(
                    '<b>Annual Production<br /><u>(TDV KWh/yr)</u></b>'
                ), '', mc_label_pp(
                    '<b>Solar PV Credit<br /><u>(TDV KBtu/sf-yr)</u></b>'
                ), '',
            ),
            # Row 6
            (
                tag_medium_center(
                    kw.get('annual_pv_production',
                           'MISSING ANNUAL PV PRODUCTION'), **kw
                ),
                mc_label_pp('<b>/ (0.293 x Conditioned Area) =</b>'),
                tag_medium_center(
                    kw.get('solar_pv_credit',
                           'MISSING SOLAR PV CREDIT'), **kw
                ), '',
            ),
            # Row 7
            (
                '', '', mc_label_pp(
                    '<i>[used in Step #3]</i>'
                ), '',
            ),
        )
        return Table(
            data,
            colWidths=(get_remaining_width(cols)),
            style=self.styles['tbl_1_pad'] + [
                ('SPAN', (0,0),(-1,0)),
                ('SPAN', (0,1),(-1,1)),
                ('SPAN', (0,2),(-1,2)),
                ('SPAN', (0,3),(-1,3)),
                ('SPAN', (0,4),(-1,4)),
                ('BOX',(0,6),(0,6),LINE_WIDTH,LINE_COLOR),
                ('BOX',(2,6),(2,6),LINE_WIDTH+1,LINE_COLOR),
            ],
        )

    def prop_design(self):
        kw = self.kwargs
        minus = mc_label_pp('<b>-</b>')
        equals = mc_label_pp('<b>=</b>')
        cols = [
            1.8*inch, 0.25*inch, 1.8*inch, 0.25*inch, 1.8*inch
        ]
        data = (
            # Row 0
            ('', '', '', '', '', '',),
            # Row 1
            (
                h2_bold_pp(
                    '<u>3. Calculate Marin County Energy Ordinance Proposed ' \
                    'Design:</u>'
                ),'','','','','',
            ),
            # Row 2
            (
                mc_label_pp(
                    '<b>Proposed Design<super>1</super><br /><u>TDV ' \
                    'KBtu/sf-yr</u></b>'
                ), '', mc_label_pp(
                    '<b>Solar PV Credit<br /><u>TDV ' \
                    'KBtu/sf-yr</u></b>'
                ), '', mc_label_pp(
                    '<b>Adjusted<br />Proposed Design<br /><u>TDV ' \
                    'KBtu/sf-yr</u></b>'
                ), ''
            ),
            # Row 3
            (
                tag_medium_center(
                    kw.get('proposed_t24',
                           'MISSING PROPOSED T24'), **kw
                ), minus,
                tag_medium_center(
                    kw.get('solar_pv_credit',
                           'MISSING SOLAR PV CREDIT'), **kw
                ), equals,
                tag_medium_center(
                        kw.get('adjusted_proposed_design',
                               'MISSING ADJUSTED PROPOSED DESIGN'), **kw), '',
            ),
            # Row 4
            (
                mc_label_pp(
                    '<i>[from Part 1, CF-1R;<br />see Note 1 below]</i>'
                ), '',
                mc_label_pp(
                    '<i>[from Step #2: enter<br />zero if no Solar PV]</i>'
                ), '',
                mc_label_pp(
                    '<i>[Used in step #4]</i>'
                ), '',
            ),
        )
        return Table(
            data,
            colWidths=(get_remaining_width(cols)),
            style=self.styles['tbl_1_pad'] + [
                ('SPAN', (0,0),(-1,0)),
                ('SPAN', (0,1),(-1,1)),
                ('BOX',(0,3),(0,3),LINE_WIDTH,LINE_COLOR),
                ('BOX',(2,3),(2,3),LINE_WIDTH,LINE_COLOR),
                ('BOX',(4,3),(4,3),LINE_WIDTH+1,LINE_COLOR),
                ('VALIGN',(0,4),(4,4),'TOP'),
            ],
        )


    def compliance_requirement(self):
        kw = self.kwargs
        cols = [
            1.8*inch, 2.3*inch, 1.8*inch
        ]
        data = (
            # Row 0
            ('', '', '', '',),
            # Row 1
            (
                h2_bold_pp(
                    "<u>4. Verify Marin County Compliance Requirement:</u>"
                ),'','','',
            ),
            # Row 2
            (
                mc_label_pp(
                    '<b>Adjusted<br />Standard Design<br /><u>(TDV KWh/yr)' \
                    '</u></b>'
                ), '', mc_label_pp(
                    '<b>Adjusted<br />Proposed Design<br /><u>(TDV KWh/yr)' \
                    '</u></b>'
                ), '',
            ),
            # Row 3
            (
                tag_medium_center(
                        kw.get('adjusted_standard_design',
                               'MISSING ADJUSTED STANDARD DESIGN'), **kw
                ),
                mc_label_pp('<b>must be = or &gt;</b>'),
                tag_medium_center(
                    kw.get('adjusted_proposed_design',
                           'MISSING ADJUSTED PROPOSED DESIGN'), **kw
                ), '',
            ),
        )
        return Table(
            data,
            colWidths=(get_remaining_width(cols)),
            style=self.styles['tbl_1_pad'] + [
                ('SPAN', (0,0),(-1,0)),
                ('SPAN', (0,1),(-1,1)),
                ('BOX',(0,3),(0,3),LINE_WIDTH+1,LINE_COLOR),
                ('BOX',(2,3),(2,3),LINE_WIDTH+1,LINE_COLOR),
            ],
        )

    def compliance_table(self):
        cols = [
            1.8*inch, 2.3*inch, 1.8*inch
        ]
        data = (
            # Row 0
            ('', '', '', '',),
            # Row 1
            (
                mc_label_pp(
                    '<b><i>Compliance Table: Adjustment Factor Based on ' \
                    'House Size and Climate Zone</i></b>'
                ), '', '', '',
            ),
            # Row 2
            (
                mc_label_pp('House Size (Total<br />Conditioned Area)'),
                mc_label_pp('Buildings Located<br />In Climate Zone 2'),
                mc_label_pp('Buildings Located<br />In Climate Zone 3'),
                '',
            ),
            # Row 3
            (mc_table_pp('&gt; 1,500 and &lt; 4,500 SF'), mc_table_pp('0.850'), mc_table_pp('0.850'), '',),
            (mc_table_pp('4,500 - 5,499 SF'), mc_table_pp('0.825'), mc_table_pp('0.850'), '',),
            (mc_table_pp('5,500 - 6,499 SF'), mc_table_pp('0.700'), mc_table_pp('0.730'), '',),
            (mc_table_pp('6,500 - 7,499 SF'), mc_table_pp('0.630'), mc_table_pp('0.665'), '',),
            (mc_table_pp('7,500 - 8,499 SF'), mc_table_pp('0.570'), mc_table_pp('0.605'), '',),
            (mc_table_pp('8,500 - 9,499 SF'), mc_table_pp('0.525'), mc_table_pp('0.560'), '',),
            (mc_table_pp('9,500 - 10,499 SF'), mc_table_pp('0.485'), mc_table_pp('0.520'), '',),
            (mc_table_pp('10,500 - 11,499 SF'), mc_table_pp('0.450'), mc_table_pp('0.485'), '',),
            (mc_table_pp('11,500+ SF'), mc_table_pp('0.425'), mc_table_pp('0.460'), mc_table_pp('<i>Revised 3/20/08</i>'),),
        )
        return Table(
            data,
            colWidths=(get_remaining_width(cols)),
            style=self.styles['tbl_1_pad'] + [
                ('SPAN', (0,0),(-1,0)),
                ('SPAN', (0,1),(-1,1)),
                ('BOX',(0,2),(2,-1),LINE_WIDTH,LINE_COLOR),
                ('INNERGRID',(0,2),(2,-1),LINE_WIDTH,LINE_COLOR),
            ],
        )

    def collect_parts(self):
        """Returns a sequence of sequences (Flowables) that can be placed in a
        one-column Table.

        container   row    col0   col1   col2   end row   end container
            (            (cell0, cell1, cell2,),            )
        """
        parts = []
        parts.append([self.title(),])
        parts.append([self.project_block(),])
        parts.append([self.std_design(),])
        parts.append([self.pv_credit(),])
        parts.append([self.prop_design(),])
        parts.append([self.compliance_requirement(),])
        parts.append([self.compliance_table(),])

        return parts
